<template>
  <!-- 通联认证签约 -->
  <div class="tl-Qualification-container">
    <!-- <div class="login-title">手机号验证</div>
    <div class="login-user login-user-style2">
      <div class="login-line">
        <div class="login-name">手机号</div>
        <div class="login-val">
          <input type="number" name="phone" placeholder="请输预留手机号" v-model="formData.phone" v-if="phone.length>0" readonly>
          <input type="number" name="phone" placeholder="请输预留手机号" v-model="formData.phone" v-else>
        </div>
      </div> -->
      
      <!-- 手机号绑定成功则不显示验证码 -->
      <!-- <div class="login-line login-line-verify" v-if="!(phone.length>0)">
        <div class="login-name">验证码</div>
        <div class="login-val">
          <input
            class="msg-verify-input"
            type="text"
            name="code"
            placeholder="请输入验证码"
            id="vcode"
            v-model="formData.verification_code"
          />
          <a class="msg-verify-btn"
              :class="verifyAtive?'active':''"
               href="javascript:;"
               @click="refreshmsgVerify">{{msgVerifyTxt}}</a>
        </div>
      </div>
    </div> -->
    <div class="login-title-center" v-if="shopping_card_recharge_transfer == 1">因为该充值为现金充值，因监管需要核定您的身份信息</div>
    <div class="login-title">绑定银行卡</div>
    <div class="login-user">
      <div class="login-line">
        <div class="login-name">持卡人姓名</div>
        <div class="login-val">
          <input type="text" name="name" placeholder="请输入姓名" v-model="formData.name" />
        </div>
      </div>
      <div class="login-line">
        <div class="login-name">持卡人身份证号</div>
        <div class="login-val">
          <input type="text" name="identity_no" placeholder="请输入身份证号" v-model="formData.identity_no" />
        </div>
      </div>
      <div class="login-line bank_card">
        <div class="login-name">银行卡号</div>
        <div class="login-val">
          <input type="number" name="cardNo" placeholder="请输入银行卡号" v-model="formData.card_no" />
        </div>
        <div class="tips">注意：请勿填写信用卡卡号</div>
      </div>
      <div class="login-line">
        <div class="login-name">预留手机号</div>
        <div class="login-val">
          <input type="number" name="bankPhone" placeholder="请输预留手机号" v-model="formData.bank_phone" />
        </div>
      </div>
    </div>
    <!-- <div class="application-txt">
      <van-checkbox v-model="agree" shape="square" checked-color="#FE1B49" icon-size="14px" class="application-checkbox">
        <span>签署即同意<span class="application-name">《在线合同签署协议》</span></span>
      </van-checkbox>
    </div> -->
    <div class="btn-box">
      <div class="submit-btn red-gra-btn" @click="clickSubmit">开始签署</div>
    </div>
  </div>
</template>

<script>
import { validLength, validateMobile } from '@/utils/validate'
import { tlGetMember, sendVerificationCode, personSignContract, signContract } from '@/api/user/member/memberApi'
import Vue from 'vue'

export default Vue.extend({
  data() {
    return {
      msgVerifyTxt: '获取验证码',
      agree: false,
      phone: '',
      shopping_card_recharge_transfer: 0,
      tlfz_have_person_signed:0, //是否绑卡/身份证/手机号
      contract_status: 0, //是否通联分账电子签约
      formData: {
        phone: '',
        verification_code: '',
        name: '',
        identity_type: 1, // 实名认证证件类型：1身份证
        identity_no: '', // 实名认证证件号
        card_no: '',
        bank_phone: ''
      },
      verifyAtive: false
    }
  },
  created() {
    this.init()
  },
  mounted() {
      window.addEventListener('wxshow', () => {
        this.init()
      })
      
  },
  methods: {
    JumpName(url,data) {
      this.$JumpName(this, url, data)
    },
    init() {
      const addType = this.$route.query.addType

      if (addType && addType == 1) {
        document.title = '添加新银行卡'
      }
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 50000)

      tlGetMember().then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          const resData = res.data
          this.shopping_card_recharge_transfer = resData.shopping_card_recharge_transfer
          this.phone = resData.phone
          this.formData.phone = resData.phone
          this.formData.verification_code = resData.verification_code || ''
          this.formData.name = resData.name
          this.formData.identity_no = resData.identity_no
          if (addType && addType == 1) {
            this.formData.card_no = ''
            this.formData.bank_phone = ''
          } else {
            this.formData.card_no = resData.tlfz_bind_bank[0] ? resData.tlfz_bind_bank[0].card_no : ''
            this.formData.bank_phone = resData.tlfz_bind_bank[0] ? resData.tlfz_bind_bank[0].bank_phone : resData.bank_phone
          }
        } else {
          this.$Error(res.msg)
        }
      })
    },
    // 获取验证码倒计时
    setTimeoutVerify() {
      let count = 60
      this.msgVerifyTxt = count + '秒后重新获取'
      const timer = setInterval(() => {
        this.msgVerifyTxt = count + '秒后重新获取'
        if (count <= 0) {
          this.msgVerifyTxt = '获取验证码'
          clearInterval(timer)
        }
        count--
      }, 1000)
    },
    // 获取短信验证码
    refreshmsgVerify() {
      const phone = this.formData.phone
      if (this.msgVerifyTxt == '获取验证码') {
        if (validateMobile(phone)) {
          // veri_code_type 验证码类型 9=>绑定手机号 6=>解绑手机号
          const data = { phone: phone, veri_code_type: 9 }
          sendVerificationCode(data).then(res => {
            if (res.status == 1) {
              this.setTimeoutVerify()
              this.$Error('发送成功')
            } else {
              this.$Error(res.msg)
            }
          })
        } else {
          this.$Error('请输入正确的手机号')
        }
      }
    },
    // 确定签约
    clickSubmit() {
      // if (!validateMobile(this.formData.phone)) {
      //   this.$Error('请输入正确的手机号')
      //   return false
      // } else
      if (!validLength(this.formData.name)) {
        this.$Error('请输入的持卡人姓名')
        return false
      } else if (!validLength(this.formData.identity_no)) {
        this.$Error('请输入的持卡人身份证号')
        return false
      } else if (!validLength(this.formData.card_no)) {
        this.$Error('请输入的银行卡号')
        return false
      } else if (!validateMobile(this.formData.bank_phone)) {
        this.$Error('请输入正确的预留手机号')
        return false
      }
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 50000)
      personSignContract(this.formData).then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          this.$Error(res.msg)
            signContract().then(res=>{
              if(res.status == 1){
                const tlfzUrl = res.data.jump_url
                // 跳转通商云平台账户的小程序
                wx.navigateToMiniProgram({
                  appId: 'wxc46c6d2eed27ca0a',//通商云平台账户小程序appId
                  path: 'pages/merchantAddress/merchantAddress',
                  extraData: {
                    targetUrl: tlfzUrl//商户拼接的请求报文
                  },
                  success(res) {
                    // 打开成功
                    // console.log(res)
                  },
                  fail(error) {
                    this.$Error('签约跳转失败')
                  }
                })
              }else{
                this.$Error(res.msg)
              }
            })
        } else {
          this.$Error(res.msg)
        }
      })
    }
  },
  watch: {
    'formData.phone'(val) {
      if (validateMobile(val)) {
        this.verifyAtive = true
      } else {
        this.verifyAtive = false
      }
    }
  }
})
</script>

<style lang="scss">
@import "src/styles/mixin";
.tl-Qualification-container {
  padding:0 24px;
  @include placeColor(#999);
  input:disabled {
    background: transparent;
  }
  .login-title{
    padding:30px 4px 20px;
    font-size:26px;
    line-height: 30px;
    color:#666;
  }
  .login-title-center{
    padding:30px 4px 20px;
    font-size:26px;
    line-height: 30px;
    color:red;
  }
  .login-user {
    background: #fff;
    width: 100%;
    overflow: hidden;
    border-radius:20px;
  }
  .login-line {
    display: flex;
    width: 100%;
    font-size: 32px;
    border-bottom: 1px solid #ededed;
    padding: 0 30px;
    box-sizing: border-box;
    &.bank_card {
      position: relative;
      // padding-top: 10px;
      padding-bottom: 20px;
      .tips {
        position: absolute;
        right: 10px;
        bottom: 6px;
        color: #FE1B49;
        font-size: 24px;
      }
    }
    &:last-child{
      border-bottom:0;
    }
    .login-name {
      width: 40%;
      line-height: 96px;
      font-size: 28px;
      color: #333333;
    }
    .login-val {
      width: 60%;
      position: relative;
      text-align:right;
      input {
        display: inline-block;
        font-size: 26px;
        color: #666666;
        border: 0;
        text-align: right;
        padding: 20px 0;
        height: 100px;
        background: transparent;
        &.birthday-input {
          padding-right: 50px;
        }
      }
      .icon_calendar {
        font-size: 36px;
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
      }
    }
    &.login-line-verify {
      .login-val{
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      .msg-verify-input {
        width: 50%;
        font-size: 26px;
        color: #999999;
      }
      .msg-verify-btn {
        display: inline-block;
        // width: 150px;
        height: 54px;
        padding:0 20px;
        line-height: 54px;
        border-radius: 54px;
        text-align: center;
        font-size: 24px;
        color: #9A9A9A;
        border:2px solid #9A9A9A;
        &.active{
          color:#F30C23;
          border-color:#F30C23;
        }
      }
    }
  }
  .login-user-style2{
    .login-line{
      .login-name{
        width:24%;
      }
      .login-val{
        width:76%;
        text-align:left;
        input{
          text-align:left;
        }
      }
    }
  }
  .application-txt{
    display: flex;
    justify-content: center;
    margin-top:34px;
    .application-name{
      color:#F11227;
    }
  }
  .btn-box {
    padding: 40px 0;
    font-size: 32px;
    text-align: center;
    .submit-btn {
      margin-bottom: 20px;
      border-radius: 92px;
      line-height: 92px;
      color: #fff;
      box-sizing: border-box;
    }
  }
}
</style>